package com.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.web.dao.AdOrderDao;
import com.web.domain.Order;
import com.web.domain.OrderItem;
import com.web.service.AdOrderService;
import com.web.service.BaseService;
import com.web.utils.BeanFactory;
import com.web.utils.JsonUtil;

/**
 * Servlet implementation class AdOrderServlet
 */
@WebServlet("/adorder") 
public class AdOrderServlet extends BaseService {
	private static final long serialVersionUID = 1L;
	private AdOrderService aos = (AdOrderService) BeanFactory.getBean("AdOrderService");
	//订单
	public String findNotPay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			int state = Integer.parseInt(request.getParameter("state"));
			request.setAttribute("state", state);
			List<Order> order = aos.findOrder(state);
			request.setAttribute("order", order);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/admin/orders/manger_order.jsp";
	}
	
	public String findProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			response.setCharacterEncoding("utf-8");
			String oid = request.getParameter("oid");
			List<OrderItem> orderItem = aos.findProduct(oid);
			if(orderItem!=null && orderItem.size()>0) {
				response.getWriter().print(JsonUtil.list2json(orderItem));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String upState(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String oid = request.getParameter("oid");
		int state = Integer.parseInt(request.getParameter("state"));
		AdOrderDao ado = (AdOrderDao) BeanFactory.getBean("AdOrderDao");
		ado.upState(oid,state);
		return findNotPay(request, response);
	}
}
